var addcion = '<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin_id coin-type" lay-filter="aihao"><option value=""></option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>'
//添加货币配置
function addCion() {
	$("#coinForm").append(addcion);
	$("#coinForm .layui-form-item:last-child").find("select.coin-type").html($("#cionHtml").html());
}

//删除货币配置
function delCion($this) {
	$this.parents(".layui-form-item").remove();
}

//根据货币ID匹配货币名称
function cinName(id) {
	var coinName;
	$.each($.parseJSON(coin), function(index, value) {
		if(value.id == id) {
			coinName = value.coin_name
		}
	});
	return coinName
}

//获取当前货币配置
function GainCoin() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=fuwu&config=coin',
		success: function(data) {
			var data = data.config_value;
			$.each(data, function(index, value) {
				$("#coinForm").append('<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin-type coin_id" lay-filter="aihao"><option value="' + value.coin_id + '">' + cinName(value.coin_id) + '</option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="' + value.config + '" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>');
			})

		}
	});
}

//根据等级ID匹配等级名称
function levelName(id) {
	var levelName;
	$.each($.parseJSON(level), function(index, value) {
		if(value.id == id) {
			levelName = value.level_name
		}
	});
	return levelName
}




//获取当前服务补贴配置
function GainLevel() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=fuwu&config=agent',
		success: function(data) {
			var data = data.config_value;
			$.each(data, function(index,value) {
				$("#butieForm").append('<div class="layui-form-item" data-id="'+value.level_id+'"><div class="layui-inline"><label class="layui-form-label">'+levelName(value.level_id)+'</label><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+value.rate+'" autocomplete="off" class="layui-input rate"><span class="layui-input-bq">%</span></div><div class="layui-form-mid">封顶值</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+value.max+'" autocomplete="off" class="layui-input max"></div></div></div>');
			})

		}
	});
}

layui.use('layer', function() {
	var $ = layui.jquery,
		layer = layui.layer;
	var active = {
		//保存货币配置
		SubmitCoin: function() {
			var Array = [];
			$("#coinForm .layui-form-item").each(function(index) {
				var coin_id = $(this).find("select.coin_id option:selected").val();
				var config = $(this).find("input.config").val();
				var data = {
					coin_id: coin_id,
					config: config
				};
				Array.push(data);
			});
			console.log(Array)
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: SubmitCoinSrc,
				data: {
					token: token,
					bonus_name: "fuwu",
					config_name: "coin",
					data: Array
				},
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			});
		},
		//保存服务补贴配置
		SubmitButie: function() {
			var arr = new Array;
			$("#butieForm .layui-form-item").each(function(index) {
					var level_id = $(this).attr("data-id");
					var rate = $(this).find("input.rate").val();
					var max = $(this).find("input.max").val();
					var data = {
						level_id: level_id,
						rate: rate,
						max: max
					};
					arr.push(data);
			});
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: SubmitCoinSrc,
				data: {
					token: token,
					bonus_name: "fuwu",
					config_name: "agent",
					data: arr
				},
				'success': function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			});

		}
	};
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),
			method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});

})

$(function() {
	cionFun();
	GainCoin();
	GainLevel();
})